package jaoso.framework.common;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * A simple util class that sends out emails
 * 
 * @author David Xu
 * @author Charles Huang
 * @version 1.1
 */
public class SendMail
{
    /**
     * DOCUMENT ME!
     * 
     * @param args
     *            DOCUMENT ME!
     */
    public static void main(String[] args)
    {
        try
        {
            SendMail
                    .send(
                            "edgeloner@163.com",
                            "pp968admin@sina.com",
                            "Notification",
                            "<html><img src='http://www.chinaxp.org/forum/images/logo.gif'><b>test</b></html>");
            System.out.println("send mail success!");
        } catch (final Exception exception)
        {
            exception.printStackTrace();
        }
    }

    /**
     * "send" method to send the message.
     * 
     * @param to
     *            To e-mail address.
     * @param from
     *            From e-mail address,optional.If not provided, will use
     *            "redsoft@chinaxp.org".
     * @param subject
     *            Subject of the mail
     * @param body
     *            Mail body
     * @exception Exception
     *                if to e-mail address is empty
     */
    public static void send(final String to, final String from,
            final String subject, final String body)
            throws javax.mail.MessagingException,
            javax.mail.internet.AddressException
    {
        //read the hardcoded values from propertiy file
        //Validation.validateNotNull( to );
        //Validation.validateNotNull( from );
        //Validation.validateNotNull( subject );
        // Validation.validateNotNull( body );
        //final String smtpServer="www.chinaxp.org";
        final String smtpServer = "smtp.sina.com.cn";
        final String user = "pp968admin";
        final String password = "6803533";

        final Properties props = new Properties();
        props.put("mail.smtp.host", smtpServer);

        //add smtp Authenticator
        props.put("mail.smtp.auth", "true");

        final Session session = Session.getDefaultInstance(props, null);
        session.setDebug(false);

        final Transport tr = session.getTransport("smtp");
        tr.connect(smtpServer, user, password);

        MimeMessage msg = new MimeMessage(session);
        final MimeBodyPart mbp1 = new MimeBodyPart();
        final MimeMultipart mp = new MimeMultipart();
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subject, "GB2312");
        mbp1.setText(body, "GB2312");
        mp.addBodyPart(mbp1);
        msg.setContent(mp);
        msg.setSentDate(new java.util.Date());
        msg.saveChanges(); // don't forget this

        //Transport.send(msg);change to use intance not use static method
        // send()
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();
    }

    /**
     * DOCUMENT ME!
     * 
     * @author $author$
     * @version $Revision: 1.1 $
     */
    class XAuthenticator extends Authenticator
    {
        /** DOCUMENT ME! */
        private String password;

        /** DOCUMENT ME! */
        private String user;

        /**
         * Creates a new XAuthenticator object.
         * 
         * @param user
         *            DOCUMENT ME!
         * @param password
         *            DOCUMENT ME!
         */
        public XAuthenticator(final String user, final String password)
        {
            // Validation.validateNotNull( user );
            //  Validation.validateNotNull( password );
        }

        /**
         * DOCUMENT ME!
         * 
         * @return DOCUMENT ME!
         */
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(user, password);
        }
    }
}